package com.umpay.privi.dao.on.sys.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import com.umpay.common.persist.AbstractDao;
import com.umpay.common.persist.ibatis.DataAccessException;
import com.umpay.common.persist.ibatis.exception.SQLErrorCodeSQLExceptionTranslator;
import com.umpay.privi.common.XmlData;
import com.umpay.privi.dao.EntityDaoImpl;
import com.umpay.privi.dao.on.sys.UserDao;
import com.umpay.privi.model.sys.User;

/** 
 * desc:用户Dao实现类
 * <p>创建人：Ding Zhe 创建日期：Apr 19, 2013 </p>
 * @version V1.0  
 */
@SuppressWarnings("unchecked")
public class UserDaoImpl extends EntityDaoImpl<User> implements UserDao {
	
	public User get(String userId)throws DataAccessException{
		try {
			return (User)getSqlMapClient().queryForObject("User.Get", userId);
		} catch (SQLException e) {
			throw new SQLErrorCodeSQLExceptionTranslator(AbstractDao.DB_TYPE_DB2).translate(e.getMessage(), e);
		}
	}
	
	public User getUser(Map<String, Object> qryMap)throws DataAccessException{
		try {
			return (User)getSqlMapClientTemplate().getSqlMapClient().queryForObject("User.GetUser", qryMap);
		} catch (SQLException e) {
			throw new SQLErrorCodeSQLExceptionTranslator(AbstractDao.DB_TYPE_DB2).translate(e.getMessage(), e);
		}
	}

	public List<User> findByRoleId(Map<String, Object> paraMap)
			throws DataAccessException {
		try {
			return (List<User>)getSqlMapClient().queryForList("User.FindByRoleId", paraMap);
		} catch (SQLException e) {
			throw new SQLErrorCodeSQLExceptionTranslator(AbstractDao.DB_TYPE_DB2).translate(e.getMessage(), e);
		}
	}
	
	public List<Map<String, Object>> queryUsingUserByRoleId(int roleId, short sysId) throws DataAccessException {
		Map<String, Object> paraMap = new HashMap<String, Object>();
		paraMap.put(XmlData.ROLE_ID, roleId);
		paraMap.put(XmlData.SYSID, sysId);
		try {
			return (List<Map<String, Object>>)getSqlMapClient().queryForList("User.queryUsingUserByRoleId", paraMap);
		} catch (SQLException e) {
			throw new SQLErrorCodeSQLExceptionTranslator(AbstractDao.DB_TYPE_DB2).translate(e.getMessage(), e);
		}
	}

	public int updateLoginTime(String sysId, String userId) throws DataAccessException {
		Map<String, Object> kMap = new Hashtable<String, Object>();
		kMap.put(User.USERID, userId);
		kMap.put(User.SYSID, sysId);
		try {
			return getSqlMapClient().update("User.UpdateLoginTime", kMap);
		} catch (SQLException e) {
			throw new SQLErrorCodeSQLExceptionTranslator(AbstractDao.DB_TYPE_DB2).translate(e.getMessage(), e);
		}
	}

	public int updateLogoutTime(String sysId, String userId) throws DataAccessException {
		Map<String, Object> kMap = new Hashtable<String, Object>();
		kMap.put(User.USERID, userId);
		kMap.put(User.SYSID, sysId);
		try {
			return getSqlMapClient().update("User.UpdateLogoutTime", kMap);
		} catch (SQLException e) {
			throw new SQLErrorCodeSQLExceptionTranslator(AbstractDao.DB_TYPE_DB2).translate(e.getMessage(), e);
		}
	}

	public List<Integer> findResourcesByUserId(String userId) throws DataAccessException {
		try {
			return getSqlMapClient().queryForList("User.findResourcesByUserId", userId);
		} catch (SQLException e) {
			throw new SQLErrorCodeSQLExceptionTranslator(AbstractDao.DB_TYPE_DB2).translate(e.getMessage(), e);
		}
	}

	public List<Integer> findMenusByUserId(String userId) throws DataAccessException {
		try {
			return getSqlMapClient().queryForList("User.findMenusByUserId", userId);
		} catch (SQLException e) {
			throw new SQLErrorCodeSQLExceptionTranslator(AbstractDao.DB_TYPE_DB2).translate(e.getMessage(), e);
		}
	}

	public List<User> findUserListByKeys(Map<String, Object> reqMap) throws DataAccessException {
		try {
			return getSqlMapClient().queryForList("User.findUserListByKeys", reqMap);
		} catch (SQLException e) {
			throw new SQLErrorCodeSQLExceptionTranslator(AbstractDao.DB_TYPE_DB2).translate(e.getMessage(), e);
		}
	}
	
	public List<Map<String,Object>> findUsersForPage(Map<String, Object> reqMap,Integer startIndex,Integer pageSize) throws DataAccessException {
		try {
			return getSqlMapClient().queryForList("User.findUserListByKeys",reqMap,startIndex,pageSize);
		} catch (SQLException e) {
			throw new SQLErrorCodeSQLExceptionTranslator(AbstractDao.DB_TYPE_DB2).translate(e.getMessage(), e);
		}
	}
	
	public Integer findUserPageList_count(Map<String, Object> reqMap) throws DataAccessException {
		try {
			return (Integer)getSqlMapClient().queryForObject("User.findUserListByKeys_count", reqMap);
		} catch (SQLException e) {
			throw new SQLErrorCodeSQLExceptionTranslator(AbstractDao.DB_TYPE_DB2).translate(e.getMessage(), e);
		}
	}
	
	public List<User> findAdminByGroupId(Map<String, Object> qryMap) throws DataAccessException {
		try {
			return (List<User>) getSqlMapClient().queryForList("User.findAdminByGroupId", qryMap);
		} catch (SQLException e) {
			throw new SQLErrorCodeSQLExceptionTranslator(AbstractDao.DB_TYPE_DB2).translate(e.getMessage(), e);
		}
	}
	
	public List<User> findNextClassAdminsByGroupId(Map<String, Object> qryMap) throws DataAccessException {
		try {
			return (List<User>)getSqlMapClient().queryForList("User.findNextClassAdminsByGroupId", qryMap);
		} catch (SQLException e) {
			throw new SQLErrorCodeSQLExceptionTranslator(AbstractDao.DB_TYPE_DB2).translate(e.getMessage(), e);
		}
	}
	
	public Integer updateUser(Map<String, Object> qryMap) throws DataAccessException {
		try {
			return (Integer) getSqlMapClient().update("User.updateUser", qryMap);
		} catch (SQLException e) {
			throw new SQLErrorCodeSQLExceptionTranslator(AbstractDao.DB_TYPE_DB2).translate(e.getMessage(), e);
		}
	}
	
	public boolean isAllUserLockInGroup(Map<String, Object>qryMap) throws DataAccessException {
		try {
			int count = (Integer) getSqlMapClient().queryForObject("User.isAllUserLockInGroup", qryMap);
			if (count != 0) {
				return false;
			} else {
				return true;
			}
		} catch (SQLException e) {
			throw new SQLErrorCodeSQLExceptionTranslator(AbstractDao.DB_TYPE_DB2).translate(e.getMessage(), e);
		}
	}
	
	public List<User> findUserPageListByUserGroup(Map<String, Object> reqMap,Integer startIndex,Integer pageSize) throws DataAccessException {
		try {
			return getSqlMapClient().queryForList("User.findUserByUserGroup",reqMap,startIndex,pageSize);
		} catch (SQLException e) {
			throw new SQLErrorCodeSQLExceptionTranslator(AbstractDao.DB_TYPE_DB2).translate(e.getMessage(), e);
		}
	}
	
	public Integer findUserPageListByUserGroup_count(Map<String, Object> reqMap)throws DataAccessException {
		try {
			return (Integer)getSqlMapClient().queryForObject("User.findUserByUserGroup_count",reqMap);
		} catch (SQLException e) {
			throw new SQLErrorCodeSQLExceptionTranslator(AbstractDao.DB_TYPE_DB2).translate(e.getMessage(), e);
		}
	}
	

}



